package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.aibkAnalysisZHT

import android.os.Handler
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.auidoPlayer.ChildAudioPlayer
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragment
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperPerAnalysisInfor
import kotlinx.android.synthetic.main.aibk_adapter_parent_xzt.view.*

/**
 * auther: 王国龙
 *  Date：2020.05.18
 * describe:综合题（一般匹配）
 */
class AnalysisFragmentParentXzt(
        val activity:AppCompatActivity,
        val paperPerAnalysisInfor: PaperPerAnalysisInfor,
        val index_A:Int,
        val index_B:Int,
        val index_C:Int
) : KBaseFragment() {

    private var rootview: View? = null

    override fun createLayout(): Int {
        return R.layout.aibk_adapter_parent_xzt
    }

    override fun initView(view: View?) {
        rootview = view

        var manager = LinearLayoutManager(activity)
        manager.orientation = LinearLayoutManager.VERTICAL
        rootview!!.RvChoice.layoutManager = manager
        val mAdapter = AnalysisAdapterChildXzt(activity,paperPerAnalysisInfor,index_A,index_B,index_C)
        mAdapter!!.setChildAudioListener(object : AnalysisAdapterChildXzt.OnChildAudioListener{
            override fun onInitChildAudioPlayerA(path: String, ivPlay: ImageView) {
                childAudioPlayer = ChildAudioPlayer(activity, path, ivPlay,
                        object : ChildAudioPlayer.PlayListener {
                            override fun onFinishPlay() {
                                childAudioPlayer!!.rePlay()
                            }

                            override fun onStartPlay() {
                                Handler().postDelayed({
                                    val audioSpeed = paperPerAnalysisInfor.TypeList[index_A].QuesDetailList[index_B].AudioSpeed
                                    childAudioPlayer!!.setPlaySpeed(audioSpeed.toFloat())
                                },100)
//                                childAudioPlayer!!.play()
                                childAudioListener!!.pauseParentAudioA(childAudioPlayer!!)
                            }

                            override fun initFirstPlay() {
                                val audioSpeed = paperPerAnalysisInfor.TypeList[index_A].QuesDetailList[index_B].AudioSpeed
                                childAudioPlayer!!.setPlaySpeed(audioSpeed.toFloat())
                                childAudioListener!!.pauseParentAudioA(childAudioPlayer!!)
                            }

                        }
                )
            }
        })
        rootview!!.RvChoice.adapter = mAdapter
    }

    interface OnChildAudioListener {
        fun pauseParentAudioA(childAudioPlayer: ChildAudioPlayer)
    }

    private var childAudioListener: OnChildAudioListener? = null

    fun setChildAudioListener(childAudioListener: OnChildAudioListener?) {
        this.childAudioListener = childAudioListener
    }

    override fun setMenuVisibility(menuVisible: Boolean) {
        super.setMenuVisibility(menuVisible)
        pauseChildAudio()
    }

    override fun onPause() {
        super.onPause()
        pauseChildAudio()
    }

}